<template>
  <div ref="chartRef1" class="chart-container">
    <div class="name">创作人：Yunzhh.com</div>
        <div class="time">当前时间：{{ currentTime }}</div>
        <!-- 添加切换背景的按钮 -->
        <div class="toggle-bg-btn">
          <button @click="toggleBackground" class="bg-toggle-button">
            {{ getButtonText() }}
          </button>
        </div>
  </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { useWindowSize } from '@vueuse/core' // 使用 vueuse 来获取窗口大小
import { sysConfig } from '../sysConfig' // 导入配置


function getCurrentDateTime() {
  const now = new Date()
  const year = now.getFullYear()
  const month = (now.getMonth() + 1).toString().padStart(2, '0')
  const day = now.getDate().toString().padStart(2, '0')
  const hours = now.getHours().toString().padStart(2, '0')
  const minutes = now.getMinutes().toString().padStart(2, '0')
  const seconds = now.getSeconds().toString().padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

const chartRef1 = ref(null)
let chartInstance = null
let intervalId = null

const { width, height } = useWindowSize()

const data = [
  '衬衫',
  '羊毛衫',
  '雪纺衫',
  '裤子',
  '高跟鞋',
  '袜子',
  '裤子1',
  '鞋子2',
  '鞋子3',
  '鞋子4'
]
const dataSeries = [5, 20, 36, 10, 10, 20, 90, 50, 60, 70]

const groupedData = []
const groupedDataSeries = []

for (let i = 0; i < data.length; i += 5) {
  groupedData.push(data.slice(i, i + 5))
  groupedDataSeries.push(dataSeries.slice(i, i + 5))
}

let currentGroupIndex = 0

const initChart1 = () => {
  chartInstance = echarts.init(chartRef1.value)
  updateChart()
  resizeChart()
  startDataSwitching()
}

const updateChart = () => {
  const option = {
    color: ['#80FFA5', '#00DDFF', '#37A2FF', '#FF0087', '#FFBF00'],

    title: {
      text: '主标题文本', // 主标题
      subtext: '副标题文本', // 副标题（可选）
      // left: 'center', // 水平位置：'left' | 'center' | 'right'
      // right: null, // 或者使用 right 设置距离右边的距离
      top: '0px', // 垂直位置：'top' | 'middle' | 'bottom' 或具体数值
      bottom: null, // 或者使用 bottom 设置距离底部的距离

      // 主标题样式
      textStyle: {
        color: '#00f2ff', // 颜色
        fontStyle: 'normal', // 字体风格：'normal' | 'italic' | 'oblique'
        fontWeight: 'bold', // 字体粗细：'normal' | 'bold' | 数值
        fontFamily: 'sans-serif', // 字体系列
        fontSize: 18, // 字体大小
        lineHeight: 30, // 行高
        textShadowBlur: 10, // 文字阴影模糊度
        textShadowColor: 'rgba(0, 242, 255, 0.7)', // 文字阴影颜色
        textShadowOffsetX: 2, // 文字阴影水平偏移
        textShadowOffsetY: 2 // 文字阴影垂直偏移
      },

      // 副标题样式
      subtextStyle: {
        color: '#00bfff',
        fontSize: 14
        // ... 其他样式属性同 textStyle
      },

      // 其他属性
      // textAlign: 'center', // 文字对齐方式
      textVerticalAlign: 'auto', // 文字垂直对齐方式
      triggerEvent: false // 是否触发事件    }
    },

    tooltip: {},
    xAxis: {
      data: groupedData[currentGroupIndex],
      axisLine: {
        lineStyle: {
          color: '#00bfff'
        }
      },
      axisLabel: {
        color: '#00bfff'
      }
    },
    yAxis: {
      axisLine: {
        lineStyle: {
          color: '#00bfff'
        }
      },
      axisLabel: {
        color: '#00bfff'
      }
    },
    series: [
      {
        name: '销量',
        type: 'bar',
        data: groupedDataSeries[currentGroupIndex],
        barWidth: 26,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#00bfff' },
            { offset: 1, color: '#1e90ff' }
          ])
        },
        label: {
          show: true,
          position: 'top',
          color: '#00bfff',
          formatter: '{c}'
        }
      }
    ],
    // graphic: [
    //   {
    //     type: 'group',
    //     right: 20,
    //     top: 20,
    //     children: [
    //       {
    //         type: 'rect',
    //         shape: {
    //           width: 30,
    //           height: 30
    //         },
    //         style: {
    //           lineWidth: 1,
    //           cursor: 'pointer',
    //           shadowBlur: 5,
    //           shadowColor: 'rgba(200, 30, 100, 0.8)'
    //         },
    //         onclick: () => {
    //           toggleFullScreen(chartRef1.value)
    //         }
    //       },
    //       {
    //         type: 'text',
    //         style: {
    //           text: '👀',
    //           textAlign: 'center',
    //           fontSize: 20,
    //           color: '#00bfff',
    //           cursor: 'pointer'
    //         },
    //         onclick: () => {
    //           toggleFullScreen(chartRef1.value)
    //         }
    //       }
    //     ]
    //   }
    // ]
  }
  chartInstance.setOption(option)
}

const resizeChart = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}

const toggleFullScreen = (element) => {
  if (!document.fullscreenElement) {
    element
      .requestFullscreen()
      .then(() => {
        element.classList.add('echarts-fullscreen')
      })
      .catch((err) => {
        console.error(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`)
      })
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen().then(() => {
        element.classList.remove('echarts-fullscreen')
      })
    }
  }
}

const startDataSwitching = () => {
  intervalId = setInterval(() => {
    currentGroupIndex = (currentGroupIndex + 1) % groupedData.length
    updateChart()
  }, sysConfig.echartsDataSwitchingInterval)
}

onMounted(() => {
  initChart1()
  window.addEventListener('resize', resizeChart)
})

onUnmounted(() => {
  window.removeEventListener('resize', resizeChart)
  if (chartInstance) {
    chartInstance.dispose()
  }
  clearInterval(intervalId)
})

watch([width, height], () => {
  resizeChart()
})
</script>

<style scoped lang="less">
/* 添加一些基本样式以确保图表容器能够正确显示 */
.chart-container {
  padding: 6% 2% 0% 2%;
  width: 100%;
  height: 100%;
}

/* 全屏时的样式 */
:deep(.echarts-fullscreen) {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background-color: #d01212;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0; /* 移除内边距 */
  margin: 0; /* 移除外边距 */
}

:deep(.echarts-fullscreen .chart) {
  width: 100%;
  height: 100%;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
</style>
